<?php
namespace app\index\controller;

use app\index\model\AccountListModel;
use app\index\model\AccountModel;
use app\index\model\BidModel;
use app\index\model\ContractShiModel;
use app\index\model\EnginBondFanModel;
use app\index\model\EnginBondShouModel;
use app\index\model\EnginBudshouModel;
use app\index\model\EnginChanModel;
use app\index\model\EnginModel;
use app\index\model\FlowBillModel;
use app\index\model\FlowLogModel;
use app\index\model\IncomeEnginModel;
use app\index\model\IncomeHuanModel;
use app\index\model\IncomeQiModel;
use app\index\model\IncomeYingModel;
use app\index\model\IncshouModel;
use app\index\model\PaymentBaoModel;
use app\index\model\PaymentGongListModel;
use app\index\model\PaymentGongModel;
use app\index\model\PaymentJieModel;
use app\index\model\PiaokaiModel;
use app\index\service\BudgetService;
use app\index\service\FlowService;
use app\index\service\IncomeService;
use app\index\service\MenuService;
use app\index\service\OptService;
use app\index\service\PaymentService;
use app\index\service\UserService;
use think\Db;
use think\Request;

class Income extends Common
{

    public function list_income_engin(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['inc.deleted_user','=', 0];
        $where[] = ['inc.comid','=',$this->userinfo['comid']];
        $where[] = ['inc.mal','=',1];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['inc.enginid','=', $param['enginid']];
        }
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|inc.number|bud.name|inc.zhai|inc.faer|inc.money|acc.accname|inc.baner','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['inc.number','like', '%'.$param['number'].'%'];
        }

        $whereBud = [];
        if(isset($param['budname']) && !empty($param['budname'])){
            if($param['budname'] == '垫资款'){
                $dzkid = (new BudgetService())->get_budshou_pid_by_name('项目收入','垫资款');
                $budshouDian = (new BudgetService())->get_budshou_sons($dzkid);
                $whereBud[] = ['inc.budid','in', $budshouDian];
            }elseif($param['budname'] == '工程款'){
                $dzkid = (new BudgetService())->get_budshou_pid_by_name('项目收入','工程款');
                $budshouDian = (new BudgetService())->get_budshou_sons($dzkid);
                $whereBud[] = ['inc.budid','in', $budshouDian];
            }elseif($param['budname'] == '质保金'){
                $dzkid = (new BudgetService())->get_budshou_pid_by_name('项目收入','质保金');
                $budshouDian = (new BudgetService())->get_budshou_sons($dzkid);
                $whereBud[] = ['inc.budid','in', $budshouDian];
            }elseif($param['budname'] == '工程款垫资款'){
                $gckid = (new BudgetService())->get_budshou_pid_by_name('项目收入','工程款');
                $gckshou = (new BudgetService())->get_budshou_sons($gckid);
                $dzkid = (new BudgetService())->get_budshou_pid_by_name('项目收入','垫资款');
                $dzkshou = (new BudgetService())->get_budshou_sons($dzkid);
                $budshouDian = array_merge($gckshou, $dzkshou);
                $whereBud[] = ['inc.budid','in', $budshouDian];
            }else{
                $where[] = ['bud.name','like', '%'.$param['budname'].'%'];
            }
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['inc.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['faer']) && !empty($param['faer'])){
            $where[] = ['inc.faer','like', '%'.$param['faer'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['inc.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['incqing']) && !empty($param['incqing'])){
            $where[] = ['inc.incqing','=', $param['incqing']];
        }
        if(isset($param['incdaotype']) && !empty($param['incdaotype'])){
            $where[] = ['inc.incdaotype','=', $param['incdaotype']];
        }
        if(isset($param['accname']) && !empty($param['accname'])){
            $where[] = ['acc.accname','like', '%'.$param['accname'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['inc.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['inc.baner','like', '%'.$param['baner'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';


        $data['incqing'] = (new OptService())->get_base_info('cai_income_incqing');
        $data['is_incqing'] = isset($param['incqing']) ? $param['incqing'] : '';

        $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
        $data['is_incdaotype'] = isset($param['incdaotype']) ? $param['incdaotype'] : '';


        $data['data'] = IncomeEnginModel::alias('inc')
            ->field('inc.*,e.name as enginname,bud.name as budname,acc.name as accname,acc.accnum')
            ->join('engin e','e.id=inc.enginid')
            ->leftJoin('engin_budshou bud','bud.id=inc.budid')
            ->leftJoin('account acc','acc.id=inc.accid')
            ->where($where)
            ->where($whereBud)
            ->where((new MenuService())->get_miss_where('income_engin', 'inc'))
            ->order('inc.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['accname'] = $item['accname'].'('.$item['accnum'].')';
                $item['incqing'] = (new OptService())->get_base_showname('cai_income_incqing', $item['incqing']);
                $item['incdaotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $item['incdaotype']);
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('income_engin');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_income_engin_kuan(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['inc.deleted_user','=', 0];
        $where[] = ['inc.comid','=',$this->userinfo['comid']];
        $where[] = ['inc.mal','=',1];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['inc.enginid','=', $param['enginid']];
        }
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|inc.number|bud.name|inc.zhai|inc.faer|inc.money|acc.accname|inc.baner','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['inc.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['budname']) && !empty($param['budname'])){
            $where[] = ['bud.name','like', '%'.$param['budname'].'%'];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['inc.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['faer']) && !empty($param['faer'])){
            $where[] = ['inc.faer','like', '%'.$param['faer'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['inc.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['incqing']) && !empty($param['incqing'])){
            $where[] = ['inc.incqing','=', $param['incqing']];
        }
        if(isset($param['incdaotype']) && !empty($param['incdaotype'])){
            $where[] = ['inc.incdaotype','=', $param['incdaotype']];
        }
        if(isset($param['accname']) && !empty($param['accname'])){
            $where[] = ['acc.accname','like', '%'.$param['accname'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['inc.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['inc.baner','like', '%'.$param['baner'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';


        $data['incqing'] = (new OptService())->get_base_info('cai_income_incqing');
        $data['is_incqing'] = isset($param['incqing']) ? $param['incqing'] : '';

        $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
        $data['is_incdaotype'] = isset($param['incdaotype']) ? $param['incdaotype'] : '';


        $data['data'] = IncomeEnginModel::alias('inc')
            ->field('inc.*,e.name as enginname,bud.name as budname,acc.name as accname,acc.accnum')
            ->join('engin e','e.id=inc.enginid')
            ->leftJoin('engin_budshou bud','bud.id=inc.budid')
            ->leftJoin('account acc','acc.id=inc.accid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('income_engin_kuan', 'inc'))
            ->order('inc.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['accname'] = $item['accname'].'('.$item['accnum'].')';
                $item['incqing'] = (new OptService())->get_base_showname('cai_income_incqing', $item['incqing']);
                $item['incdaotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $item['incdaotype']);
                return $item;
            })->toArray();
        $data['butauth'] = (new MenuService())->get_butauth('income_engin_kuan');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_income_engin(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['enginid'])){
                return outputJson(4000, '请选择项目名称');
            }
            if(empty($param['budid'])){
                return outputJson(4000, '请选择款项类型');
            }
            if(empty($param['zhai'])){
                return outputJson(4000, '请输入款项摘要');
            }
            if(empty($param['faer'])){
                return outputJson(4000, '请输入收款对象');
            }
            if(empty($param['money'])){
                return outputJson(4000, '请输入收款金额');
            }
            if(empty($param['incdaotype'])){
                return outputJson(4000, '请选择到账方式');
            }
            if(empty($param['accid'])){
                return outputJson(4000, '请选择到账账户');
            }
            if(empty($param['dt'])){
                return outputJson(4000, '请选择到账日期');
            }
            if(empty($param['baner'])){
                return outputJson(4000, '请输入经办人');
            }

            $param['mal'] = 1;
            $param['number'] = (new IncomeService())->create_income_engin_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            Db::startTrans();
            try {
                $id = IncomeEnginModel::insertGetId($param);
                (new IncomeService())->create_account_income_engin($id, $param);
                (new OptService())->relation_file($files,'income_engin',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'income_engin', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'income_engin', $id, '项目收款');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $yingid = $request->get('yingid',0,'int');
            $yingdata = IncomeYingModel::where(['id'=>$yingid])->find();
            $data['yingdata'] = $yingdata;

            $data['subarr'] = (new BudgetService())->get_engin_budshou_engin_all_xiala();
            $data['incqing'] = (new OptService())->get_base_info('cai_income_incqing');
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
            $data['number'] = (new IncomeService())->create_income_engin_number();
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_income_engin(Request $request){
        if($request->isPost()){
            $param = $request->post();


            if(empty($param['budid'])){
                return outputJson(4000, '请选择款项类型');
            }
            if(empty($param['zhai'])){
                return outputJson(4000, '请输入款项摘要');
            }
            if(empty($param['faer'])){
                return outputJson(4000, '请输入收款对象');
            }
            if(empty($param['money'])){
                return outputJson(4000, '请输入收款金额');
            }
            if(empty($param['incdaotype'])){
                return outputJson(4000, '请选择到账方式');
            }
            if(empty($param['accid'])){
                return outputJson(4000, '请选择到账账户');
            }
            if(empty($param['dt'])){
                return outputJson(4000, '请选择到账日期');
            }
            if(empty($param['baner'])){
                return outputJson(4000, '请输入经办人');
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            Db::startTrans();
            try {
                IncomeEnginModel::update($param);
                (new IncomeService())->update_account_income_engin($param['id'], $param);
                (new OptService())->relation_file($files,'income_engin',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'income_engin', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'income_engin', $param['id'], '项目收款');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = IncomeEnginModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            $engininfo['shiname'] = ContractShiModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$engininfo['id'],'deleted_user'=>0,'isturn'=>1])
                ->where('status','in',[0,1])->value('name');
            $data['engininfo'] = $engininfo;

            $kaiinfo = PiaokaiModel::get_info_by_pk($data['data']['kaiid']);
            $data['kaiinfo'] = $kaiinfo;


            $account = AccountModel::get_info_by_pk($data['data']['accid']);
            $data['account'] = $account;

            $budshou = EnginBudshouModel::get_info_by_pk($data['data']['budid']);
            $data['budshou'] = $budshou;

            $data['subarr'] = (new BudgetService())->get_engin_budshou_engin_all_xiala();
            $data['incqing'] = (new OptService())->get_base_info('cai_income_incqing');
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
            $data['files'] = (new OptService())->get_file('income_engin', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_income_engin(Request $request){
        $param = $request->post();
        $data = IncomeEnginModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            IncomeEnginModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new IncomeService())->del_account_income_engin($param['id'], $param);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'income_engin', $data['id'], '项目收款');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_income_engin(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = IncomeEnginModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
        $engininfo['shiname'] = ContractShiModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$engininfo['id'],'deleted_user'=>0,'isturn'=>1])
            ->where('status','in',[0,1])->value('name');
        $data['engininfo'] = $engininfo;

        $kaiinfo = PiaokaiModel::get_info_by_pk($data['data']['kaiid']);
        $data['kaiinfo'] = $kaiinfo;

        $account = AccountModel::get_info_by_pk($data['data']['accid']);
        $data['account'] = $account;

        $gckid = (new BudgetService())->get_budshou_pid_by_name('项目收入','工程款');
        $budshouIdArr = (new BudgetService())->get_budshou_sons($gckid);


        $budshou = EnginBudshouModel::get_info_by_pk($data['data']['budid']);
        $data['budshou'] = $budshou;



        $data['data']['lei_huikuan'] = IncomeEnginModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
            ->where(['enginid'=>$data['data']['enginid']])->where('budid','in',$budshouIdArr)->sum('money');

        $dzkid = (new BudgetService())->get_budshou_pid_by_name('项目收入','垫资款');
        $budshouDian = (new BudgetService())->get_budshou_sons($dzkid);
        $data['data']['lei_dian'] = IncomeEnginModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
            ->where(['enginid'=>$data['data']['enginid']])->where('budid','in',$budshouDian)->sum('money');


        $data['data']['lei_piaokai'] = PiaokaiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
            ->where(['enginid'=>$data['data']['enginid'],'status'=>1,'isturn'=>1])
            ->where("dt is not null and dt != 0000-00-00 and dt != ''")->sum('money');


        $data['data']['lei_chan'] = EnginChanModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
            ->where(['enginid'=>$data['data']['enginid'],'status'=>1,'isturn'=>1])->sum('leijin');

        $data['data']['huibi'] = 0;
        if($data['data']['lei_huikuan'] != 0 && $data['data']['lei_chan'] != 0){
            $data['data']['huibi'] = round($data['data']['lei_huikuan']/$data['data']['lei_chan']*100, 2);
        }


        $data['data']['incqing'] = (new OptService())->get_base_showname('cai_income_incqing', $data['data']['incqing']);
        $data['data']['incdaotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $data['data']['incdaotype']);
        $data['files'] = (new OptService())->get_file('income_engin', $data['data']['id']);
        return outputJson(2000,lang('code_2000'), $data);
    }



    public function list_income_engin_dai(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['inc.deleted_user','=', 0];
        $where[] = ['inc.comid','=',$this->userinfo['comid']];
        $where[] = ['inc.mal','=',0];


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|inc.number|bud.name|inc.zhai|inc.faer|inc.money|acc.accname|inc.baner','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['inc.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['inc.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['faer']) && !empty($param['faer'])){
            $where[] = ['inc.faer','like', '%'.$param['faer'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['inc.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['incqing']) && !empty($param['incqing'])){
            $where[] = ['inc.incqing','=', $param['incqing']];
        }
        if(isset($param['incdaotype']) && !empty($param['incdaotype'])){
            $where[] = ['inc.incdaotype','=', $param['incdaotype']];
        }
        if(isset($param['accname']) && !empty($param['accname'])){
            $where[] = ['acc.accname','like', '%'.$param['accname'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['inc.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['inc.baner','like', '%'.$param['baner'].'%'];
        }


        $data['incqing'] = (new OptService())->get_base_info('cai_income_incqing');
        $data['is_incqing'] = isset($param['incqing']) ? $param['incqing'] : '';

        $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
        $data['is_incdaotype'] = isset($param['incdaotype']) ? $param['incdaotype'] : '';


        $data['data'] = IncomeEnginModel::alias('inc')
            ->field('inc.*,acc.name as accname,acc.accnum')
            ->leftJoin('account acc','acc.id=inc.accid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('income_engin_dai', 'inc'))
            ->order('inc.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['accname'] = $item['accname'].'('.$item['accnum'].')';
                $item['incqing'] = (new OptService())->get_base_showname('cai_income_incqing', $item['incqing']);
                $item['incdaotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $item['incdaotype']);
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('income_engin_dai');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_income_engin_dai(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['zhai'])){
                return outputJson(4000, '请输入款项摘要');
            }
            if(empty($param['faer'])){
                return outputJson(4000, '请输入收款对象');
            }
            if(empty($param['money'])){
                return outputJson(4000, '请输入到账金额');
            }
            if(empty($param['incdaotype'])){
                return outputJson(4000, '请选择到账方式');
            }
            if(empty($param['accid'])){
                return outputJson(4000, '请选择到账账户');
            }
            if(empty($param['dt'])){
                return outputJson(4000, '请选择到账日期');
            }
            if(empty($param['baner'])){
                return outputJson(4000, '请输入经办人');
            }


            $param['budid'] = 0;
            $param['mal'] = 0;
            $param['number'] = (new IncomeService())->create_income_engin_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);



            Db::startTrans();
            try {
                $id = IncomeEnginModel::insertGetId($param);
                (new IncomeService())->create_account_income_engin($id, $param);
                (new OptService())->relation_file($files,'income_engin',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'income_engin', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'income_engin', $id, '项目收款');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['dt'] = date("Y-m-d");
            $data['baner'] = $this->userinfo['realname'];
            $data['incqing'] = (new OptService())->get_base_info('cai_income_incqing');
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
            $data['number'] = (new IncomeService())->create_income_engin_number();
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_income_engin_dai(Request $request){
        if($request->isPost()){
            $param = $request->post();


            if(empty($param['zhai'])){
                return outputJson(4000, '请输入款项摘要');
            }
            if(empty($param['faer'])){
                return outputJson(4000, '请输入收款对象');
            }
            if(empty($param['money'])){
                return outputJson(4000, '请输入收款金额');
            }
            if(empty($param['incdaotype'])){
                return outputJson(4000, '请选择到账方式');
            }
            if(empty($param['accid'])){
                return outputJson(4000, '请选择到账账户');
            }
            if(empty($param['dt'])){
                return outputJson(4000, '请选择到账日期');
            }
            if(empty($param['baner'])){
                return outputJson(4000, '请输入经办人');
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            if($param['enginid'] > 0){
                $param['mal'] = 1;
            }

            Db::startTrans();
            try {
                IncomeEnginModel::update($param);
                (new IncomeService())->update_account_income_engin($param['id'], $param);
                (new OptService())->relation_file($files,'income_engin',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'income_engin', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'income_engin', $param['id'], '项目收款');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = IncomeEnginModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $kaiinfo = PiaokaiModel::get_info_by_pk($data['data']['kaiid']);
            $data['kaiinfo'] = $kaiinfo;

            $account = AccountModel::get_info_by_pk($data['data']['accid']);
            $data['account'] = $account;

            $data['incqing'] = (new OptService())->get_base_info('cai_income_incqing');
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
            $data['files'] = (new OptService())->get_file('income_engin', $data['data']['id']);
            $data['subarr'] = (new BudgetService())->get_engin_budshou_engin_all_xiala();
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function ren_income_engin_dai(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['budid'])){
                return outputJson(4000, '请选择款项类型');
            }
            if(empty($param['enginid'])){
                return outputJson(4000, '请选择项目名称');
            }


            $param['mal'] = 1;
            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            unset($param['token']);



            Db::startTrans();
            try {
                IncomeEnginModel::update($param);
                AccountListModel::where(['type'=>1,'mid'=>$param['id']])->update([
                    'budid' => $param['budid'],
                    'enginid' => $param['enginid'],
                    'kaiid' => $param['kaiid'],
                    'enginname' => EnginModel::where(['id'=>$param['enginid']])->value('name')
                ]);

                (new OptService())->add_flow_log('项目来款认领', 'income_engin', $param['id']);
                (new OptService())->add_log_list('项目来款认领', 'income_engin', $param['id'], '项目收款');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = IncomeEnginModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $account = AccountModel::get_info_by_pk($data['data']['accid']);
            $data['account'] = $account;
            $data['subarr'] = (new BudgetService())->get_engin_budshou_engin_all_xiala();
            $data['incqing'] = (new OptService())->get_base_info('cai_income_incqing');
            $data['data']['incdaotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $data['data']['incdaotype']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_income_engin_dai(Request $request){
        $param = $request->post();
        $data = IncomeEnginModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            IncomeEnginModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new IncomeService())->del_account_income_engin($param['id'], $param);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'income_engin', $data['id'], '项目收款');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_income_engin_dai(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = IncomeEnginModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $account = AccountModel::get_info_by_pk($data['data']['accid']);
        $data['account'] = $account;

        $data['data']['incqing'] = (new OptService())->get_base_showname('cai_income_incqing', $data['data']['incqing']);
        $data['data']['incdaotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $data['data']['incdaotype']);
        $data['files'] = (new OptService())->get_file('income_engin', $data['data']['id']);
        return outputJson(2000,lang('code_2000'), $data);
    }

    public function list_income_ying(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['ying.deleted_user','=', 0];
        $where[] = ['ying.comid','=',$this->userinfo['comid']];


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['ying.number|ying.faer|ying.zhai|ying.money','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['ying.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['shoutype']) && !empty($param['shoutype'])){
            $where[] = ['ying.shoutype','=', $param['shoutype']];
        }
        if(isset($param['faer']) && !empty($param['faer'])){
            $where[] = ['ying.faer','like', '%'.$param['faer'].'%'];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['ying.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['ying.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['incqing']) && !empty($param['incqing'])){
            $where[] = ['ying.incqing','=', $param['incqing']];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['ying.dt','between', explode(' - ', $param['dt'])];
        }

        $data['incqing'] = (new OptService())->get_base_info('cai_income_incqing');
        $data['is_incqing'] = isset($param['incqing']) ? $param['incqing'] : '';

        $data['shoutype'] = (new IncomeService())->get_income_shoutype();
        $data['is_shoutype'] = isset($param['shoutype']) ? $param['shoutype'] : '';


        $data['data'] = IncomeYingModel::alias('ying')
            ->field('ying.*')
            ->where($where)
            ->where((new MenuService())->get_miss_where('income_ying', 'ying'))
            ->order('ying.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['incqing'] = (new OptService())->get_base_showname('cai_income_incqing', $item['incqing']);
                $item['shoutype_name'] = (new IncomeService())->get_income_shoutype_name($item['shoutype']);
                $item['daomoney'] = (new IncomeService())->get_income_ying_daomoney($item);
                $item['nodaomoney'] = $item['money'] - $item['daomoney'];
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('income_ying');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_income_ying_my(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['ying.deleted_user','=', 0];
        $where[] = ['ying.comid','=',$this->userinfo['comid']];
        $where[] = ['ying.created_user','=',$this->userinfo['id']];


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['ying.number|ying.faer|ying.zhai|ying.money','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['ying.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['shoutype']) && !empty($param['shoutype'])){
            $where[] = ['ying.shoutype','=', $param['shoutype']];
        }
        if(isset($param['faer']) && !empty($param['faer'])){
            $where[] = ['ying.faer','like', '%'.$param['faer'].'%'];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['ying.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['ying.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['incqing']) && !empty($param['incqing'])){
            $where[] = ['ying.incqing','=', $param['incqing']];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['ying.dt','between', explode(' - ', $param['dt'])];
        }

        $data['incqing'] = (new OptService())->get_base_info('cai_income_incqing');
        $data['is_incqing'] = isset($param['incqing']) ? $param['incqing'] : '';

        $data['shoutype'] = (new IncomeService())->get_income_shoutype();
        $data['is_shoutype'] = isset($param['shoutype']) ? $param['shoutype'] : '';


        $data['data'] = IncomeYingModel::alias('ying')
            ->field('ying.*')
            ->where($where)
            ->where((new MenuService())->get_miss_where('my_income_ying', 'ying'))
            ->order('ying.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['incqing'] = (new OptService())->get_base_showname('cai_income_incqing', $item['incqing']);
                $item['shoutype_name'] = (new IncomeService())->get_income_shoutype_name($item['shoutype']);
                $item['daomoney'] = (new IncomeService())->get_income_ying_daomoney($item);
                $item['nodaomoney'] = $item['money'] - $item['daomoney'];
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('my_income_ying');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_income_ying(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['shoutype'])){
                return outputJson(4000, '请选择应收类别');
            }
            if(empty($param['faer'])){
                return outputJson(4000, '请输入收款对象');
            }
            if(empty($param['zhai'])){
                return outputJson(4000, '请输入款项摘要');
            }
            if(empty($param['money'])){
                return outputJson(4000, '请输入应收金额');
            }


            $param['number'] = (new IncomeService())->create_income_ying_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            Db::startTrans();
            try {
                $id = IncomeYingModel::insertGetId($param);
                (new OptService())->relation_file($files,'income_ying',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'income_ying', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'income_ying', $id, '应收登记');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['incqing'] = (new OptService())->get_base_info('cai_income_incqing');
            $data['shoutype'] = (new IncomeService())->get_income_shoutype();
            $data['number'] = (new IncomeService())->create_income_ying_number();
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_income_ying(Request $request){
        if($request->isPost()){
            $param = $request->post();


            if(empty($param['shoutype'])){
                return outputJson(4000, '请选择应收类别');
            }
            if(empty($param['faer'])){
                return outputJson(4000, '请输入收款对象');
            }
            if(empty($param['zhai'])){
                return outputJson(4000, '请输入款项摘要');
            }
            if(empty($param['money'])){
                return outputJson(4000, '请输入应收金额');
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            Db::startTrans();
            try {
                IncomeYingModel::update($param);
                (new OptService())->relation_file($files,'income_ying',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'income_ying', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'income_ying', $param['id'], '应收登记');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = IncomeYingModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['incqing'] = (new OptService())->get_base_info('cai_income_incqing');
            $data['shoutype'] = (new IncomeService())->get_income_shoutype();
            $data['files'] = (new OptService())->get_file('income_ying', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_income_ying(Request $request){
        $param = $request->post();
        $data = IncomeYingModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            IncomeYingModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'income_ying', $data['id'], '应收登记');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_income_ying(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = IncomeYingModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }
        $data['data']['daomoney'] = (new IncomeService())->get_income_ying_daomoney($data['data']);
        $data['data']['incqing'] = (new OptService())->get_base_showname('cai_income_incqing', $data['data']['incqing']);
        $data['data']['shoutype_name'] = (new IncomeService())->get_income_shoutype_name($data['data']['shoutype']);
        $data['files'] = (new OptService())->get_file('income_ying', $data['data']['id']);
        return outputJson(2000,lang('code_2000'), $data);
    }
    public function shoutype_income_ying(Request $request){
        $id = $request->param('id',0,'int');
        $info = IncomeYingModel::where(['id'=>$id])->find();
        if(!$info){
            return outputJson(4000, lang('code_4000'));
        }
        $data['shoutype'] = $info['shoutype'];
        return outputJson(2000,lang('code_2000'), $data);
    }


    public function list_income_qi(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['inc.deleted_user','=', 0];
        $where[] = ['inc.comid','=',$this->userinfo['comid']];
        $where[] = ['inc.isturn','=',1];
        $where[] = ['inc.status','in',[0,1]];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['inc.number|bud.name|inc.zhai|inc.faer|inc.money|acc.accname|inc.baner','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['inc.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['budname']) && !empty($param['budname'])){
            $where[] = ['bud.name','like', '%'.$param['budname'].'%'];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['inc.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['faer']) && !empty($param['faer'])){
            $where[] = ['inc.faer','like', '%'.$param['faer'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['inc.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['incqing']) && !empty($param['incqing'])){
            $where[] = ['inc.incqing','=', $param['incqing']];
        }
        if(isset($param['incdaotype']) && !empty($param['incdaotype'])){
            $where[] = ['inc.incdaotype','=', $param['incdaotype']];
        }
        if(isset($param['accname']) && !empty($param['accname'])){
            $where[] = ['acc.accname','like', '%'.$param['accname'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['inc.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['inc.baner','like', '%'.$param['baner'].'%'];
        }



        $data['incqing'] = (new OptService())->get_base_info('cai_income_incqing');
        $data['is_incqing'] = isset($param['incqing']) ? $param['incqing'] : '';

        $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
        $data['is_incdaotype'] = isset($param['incdaotype']) ? $param['incdaotype'] : '';


        $data['data'] = IncomeQiModel::alias('inc')
            ->field('inc.*,bud.name as budname,acc.name as accname,acc.accnum')
            ->leftJoin('engin_budshou bud','bud.id=inc.budid')
            ->leftJoin('account acc','acc.id=inc.accid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('income_qi', 'inc'))
            ->order('inc.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['accname'] = $item['accname'].'('.$item['accnum'].')';
                $item['incqing'] = (new OptService())->get_base_showname('cai_income_incqing', $item['incqing']);
                $item['incdaotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $item['incdaotype']);
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('income_qi');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_income_qi(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['budid'])){
                return outputJson(4000, '请选择款项类型');
            }
            if(empty($param['faer'])){
                return outputJson(4000, '请输入收款对象');
            }
            if(empty($param['money'])){
                return outputJson(4000, '请输入收款金额');
            }
            if(empty($param['incdaotype'])){
                return outputJson(4000, '请选择到账方式');
            }
            if(empty($param['accid'])){
                return outputJson(4000, '请选择到账账户');
            }
            if(empty($param['dt'])){
                return outputJson(4000, '请选择到账日期');
            }
            if(empty($param['baner'])){
                return outputJson(4000, '请输入经办人');
            }

            $param['number'] = (new IncomeService())->create_income_qi_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['status'] = 1;
            $param['isturn'] = 1;
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            Db::startTrans();
            try {
                $id = IncomeQiModel::insertGetId($param);
                (new IncomeService())->create_account_income_qi($id, $param);
                (new OptService())->relation_file($files,'income_qi',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'income_qi', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'income_qi', $id, '其他收款');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $yingid = $request->get('yingid',0,'int');
            $yingdata = IncomeYingModel::where(['id'=>$yingid])->find();
            $data['yingdata'] = $yingdata;

            $data['subarr'] = (new BudgetService())->get_engin_budshou_qi_all_xiala();

            $data['dt'] = date("Y-m-d");
            $data['baner'] = $this->userinfo['realname'];
            $data['incqing'] = (new OptService())->get_base_info('cai_income_incqing');
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
            $data['number'] = (new IncomeService())->create_income_qi_number();
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_income_qi(Request $request){
        if($request->isPost()){
            $param = $request->post();


            if(empty($param['budid'])){
                return outputJson(4000, '请选择款项类型');
            }
            if(empty($param['faer'])){
                return outputJson(4000, '请输入收款对象');
            }
            if(empty($param['money'])){
                return outputJson(4000, '请输入收款金额');
            }
            if(empty($param['incdaotype'])){
                return outputJson(4000, '请选择到账方式');
            }
            if(empty($param['accid'])){
                return outputJson(4000, '请选择到账账户');
            }
            if(empty($param['dt'])){
                return outputJson(4000, '请选择到账日期');
            }
            if(empty($param['baner'])){
                return outputJson(4000, '请输入经办人');
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            Db::startTrans();
            try {
                IncomeQiModel::update($param);
                (new IncomeService())->update_account_income_qi($param['id'], $param);
                (new OptService())->relation_file($files,'income_qi',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'income_qi', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'income_qi', $param['id'], '其他收款');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = IncomeQiModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }


            $account = AccountModel::get_info_by_pk($data['data']['accid']);
            $data['account'] = $account;

            $budshou = EnginBudshouModel::get_info_by_pk($data['data']['budid']);
            $data['budshou'] = $budshou;

            $data['subarr'] = (new BudgetService())->get_engin_budshou_qi_all_xiala();
            $data['incqing'] = (new OptService())->get_base_info('cai_income_incqing');
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
            $data['files'] = (new OptService())->get_file('income_qi', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_income_qi(Request $request){
        $param = $request->post();
        $data = IncomeQiModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            IncomeQiModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new IncomeService())->del_account_income_qi($data['id']);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'income_qi', $data['id'], '其他收款');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_income_qi(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = IncomeQiModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }


        $account = AccountModel::get_info_by_pk($data['data']['accid']);
        $data['account'] = $account;

        $budshou = EnginBudshouModel::get_info_by_pk($data['data']['budid']);
        $data['budshou'] = $budshou;

        $data['data']['incqing'] = (new OptService())->get_base_showname('cai_income_incqing', $data['data']['incqing']);
        $data['data']['incdaotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $data['data']['incdaotype']);
        $data['files'] = (new OptService())->get_file('income_qi', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }



    public function list_engin_bond_shou(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['shou.deleted_user','=', 0];
        $where[] = ['shou.comid','=',$this->userinfo['comid']];
        $where[] = ['shou.isturn','=',1];
        $where[] = ['shou.status','in',[0,1]];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|shou.number|shou.zhai|shou.faer|shou.money|acc.accname|shou.baner','like', '%'.$param['keyword'].'%'];
        }

         if(isset($param['bondtype']) && !empty($param['bondtype'])){
             $where[] = ['shou.bondtype','=', $param['bondtype']];
         }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name|bid.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number|bid.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['shou.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['shou.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['shou.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['shou.gonger','like', '%'.$param['gonger'].'%'];
        }
        if(isset($param['incdaotype']) && !empty($param['incdaotype'])){
            $where[] = ['shou.incdaotype','=', $param['incdaotype']];
        }
        if(isset($param['accname']) && !empty($param['accname'])){
            $where[] = ['acc.accname','like', '%'.$param['accname'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['shou.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['shou.baner','like', '%'.$param['baner'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';




        $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
        $data['is_incdaotype'] = isset($param['incdaotype']) ? $param['incdaotype'] : '';


        $data['data'] = EnginBondShouModel::alias('shou')
            ->field('shou.*,e.name as enginname,acc.name as accname,acc.accnum,bid.name as bidname')
            ->leftJoin('engin e','e.id=shou.enginid')
            ->leftJoin('bid bid','bid.id=shou.bidid')
            ->leftJoin('account acc','acc.id=shou.accid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin_bond_shou', 'shou'))
            ->order('shou.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                if($item['bidid'] > 0){
                    $item['enginname'] = $item['bidname'];
                }
                $item['bondtype'] = (new PaymentService())->get_engin_bondtype_name($item['bondtype']);
                $item['budid_name'] = (new BudgetService())->get_budid_quanname($item['budid']);
                $item['accname'] = $item['accname'].'('.$item['accnum'].')';
                $item['incdaotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $item['incdaotype']);

                $fanId = EnginBondFanModel::where(['shouid'=>$item['id'],'deleted_user'=>0,'isturn'=>1,'status'=>1])->column('id');
                $item['fanmoney'] = AccountListModel::where(['type'=>10])->where('mid','in',$fanId)->sum('money');

                $item['daifanmoney'] = $item['money'] - $item['fanmoney'];
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('engin_bond_shou');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_engin_bond_shou(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['bondtype'] == 1){
                if(empty($param['bidid'])){
                    return outputJson(4000, '请选择项目名称');
                }
            }else{
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
            }

            if(empty($param['money'])){
                return outputJson(4000, '请输入收款金额');
            }
            if(empty($param['zhai'])){
                return outputJson(4000, '请输入款项摘要');
            }
            if(empty($param['gonger'])){
                return outputJson(4000, '请输入收款对象');
            }
            if(empty($param['budid'])){
                return outputJson(4000, '请选择款项类型');
            }
            if(empty($param['incdaotype'])){
                return outputJson(4000, '请选择到账方式');
            }
            if(empty($param['accid'])){
                return outputJson(4000, '请选择到账账户');
            }
            if(empty($param['dt'])){
                return outputJson(4000, '请选择到账日期');
            }
            if(empty($param['baner'])){
                return outputJson(4000, '请输入经办人');
            }

            $param['number'] = (new IncomeService())->create_bond_shou_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['status'] = 1;
            $param['isturn'] = 1;
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            Db::startTrans();
            try {
                $id = EnginBondShouModel::insertGetId($param);
                (new IncomeService())->create_bond_shou_engin($id, $param);
                (new OptService())->relation_file($files,'engin_bond_shou',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'engin_bond_shou', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'engin_bond_shou', $id, '保证金收取');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $yingid = $request->get('yingid',0,'int');
            $yingdata = IncomeYingModel::where(['id'=>$yingid])->find();
            $data['yingdata'] = $yingdata;

            $data['dt'] = date("Y-m-d");
            $data['baner'] = $this->userinfo['realname'];
            $data['bondtype'] = (new PaymentService())->get_engin_bondtype();
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
            $data['number'] = (new IncomeService())->create_bond_shou_number();
            $data['subarr'] = (new BudgetService())->get_engin_budshou_qi_all_xiala();
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_engin_bond_shou(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['money'])){
                return outputJson(4000, '请输入收款金额');
            }
            if(empty($param['zhai'])){
                return outputJson(4000, '请输入款项摘要');
            }
            if(empty($param['gonger'])){
                return outputJson(4000, '请输入收款对象');
            }
            if(empty($param['budid'])){
                return outputJson(4000, '请选择款项类型');
            }
            if(empty($param['incdaotype'])){
                return outputJson(4000, '请选择到账方式');
            }
            if(empty($param['accid'])){
                return outputJson(4000, '请选择到账账户');
            }
            if(empty($param['dt'])){
                return outputJson(4000, '请选择到账日期');
            }
            if(empty($param['baner'])){
                return outputJson(4000, '请输入经办人');
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            Db::startTrans();
            try {
                EnginBondShouModel::update($param);

                $newinfo = EnginBondShouModel::where(['id'=>$param['id']])->find();

                (new IncomeService())->update_bond_shou_engin($param['id'], $newinfo);
                (new OptService())->relation_file($files,'engin_bond_shou',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'engin_bond_shou', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'engin_bond_shou', $param['id'], '保证金收取');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = EnginBondShouModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            if($data['data']['enginid'] > 0){
                $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            }else{
                $engininfo = BidModel::get_info_by_pk($data['data']['bidid']);
            }
            $data['engininfo'] = $engininfo;


            $account = AccountModel::get_info_by_pk($data['data']['accid']);
            $data['account'] = $account;

            $budshou = EnginBudshouModel::get_info_by_pk($data['data']['budid']);
            $data['budshou'] = $budshou;

            $data['bondtype'] = (new PaymentService())->get_engin_bondtype();
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
            $data['subarr'] = (new BudgetService())->get_engin_budshou_qi_all_xiala();
            $data['files'] = (new OptService())->get_file('engin_bond_shou', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_engin_bond_shou(Request $request){
        $param = $request->post();
        $data = EnginBondShouModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            EnginBondShouModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new IncomeService())->del_bond_shou_engin($data['id']);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'engin_bond_shou', $data['id'], '保证金收取');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_engin_bond_shou(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = EnginBondShouModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        if($data['data']['enginid'] > 0){
            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
        }else{
            $engininfo = BidModel::get_info_by_pk($data['data']['bidid']);
        }
        $data['engininfo'] = $engininfo;


        $account = AccountModel::get_info_by_pk($data['data']['accid']);
        $data['account'] = $account;

        $budshou = EnginBudshouModel::get_info_by_pk($data['data']['budid']);
        $data['budshou'] = $budshou;

        $data['data']['bondtype'] = (new PaymentService())->get_engin_bondtype_name($data['data']['bondtype']);
        $data['data']['incdaotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $data['data']['incdaotype']);
        $data['files'] = (new OptService())->get_file('engin_bond_shou', $data['data']['id']);
        return outputJson(2000,lang('code_2000'), $data);
    }
    public function list_engin_bond_shou_kuan_detail(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['list.comid','=',$this->userinfo['comid']];
        $where[] = ['list.type','=',9];
        $where[] = ['list.mid','=',$param['mid']];


        if(isset($param['jiaonum']) && !empty($param['jiaonum'])){
            $where[] = ['list.jiaonum','like', '%'.$param['jiaonum'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['list.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['list.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['faer']) && !empty($param['faer'])){
            $where[] = ['list.faer','like', '%'.$param['faer'].'%'];
        }
        if(isset($param['daotype']) && !empty($param['daotype'])){
            $where[] = ['list.daotype','like', '%'.$param['daotype'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['list.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['accname']) && !empty($param['accname'])){
            $where[] = ['acc.accname','like', '%'.$param['accname'].'%'];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['list.baner','like', '%'.$param['baner'].'%'];
        }


        $data['data'] = AccountListModel::alias('list')
            ->field('list.*,acc.name as accname,acc.accnum')
            ->leftJoin('account acc','acc.id=list.accid')
            ->where($where)
            ->order('list.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['budid_name'] = (new BudgetService())->get_budid_quanname($item['budid']);
                $item['accname'] = $item['accname'].'('.$item['accnum'].')';
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_engin_bond_shou_kuan(Request $request){
        if($request->isPost()){
            $param = $request->post();

            $payinfo = EnginBondShouModel::where(['id'=>$param['id']])->find();

            if(empty($param['budid'])){
                return outputJson(4000, '请选择款项类型');
            }
            if(empty($param['fumoney'])){
                return outputJson(4000, '请输入本次到账金额');
            }
            if(empty($param['incdaotype'])){
                return outputJson(4000, '请选择到账方式');
            }
            if(empty($param['accid'])){
                return outputJson(4000, '请选择到账账户');
            }
            if(empty($param['dt'])){
                return outputJson(4000, '请选择到账日期');
            }
            if(empty($param['baner'])){
                return outputJson(4000, '请输入经手人');
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);

            $accInfo = [];
            $accInfo['comid'] = $this->userinfo['comid'];
            $accInfo['type'] = 9;
            $accInfo['mid'] = $param['id'];
            $accInfo['bidid'] = $payinfo['bidid'];
            $accInfo['enginid'] = $payinfo['enginid'];
            if($payinfo['bidid'] > 0){
                $accInfo['enginname'] = BidModel::where(['id'=>$payinfo['bidid']])->value('name');
            }else{
                $accInfo['enginname'] = EnginModel::where(['id'=>$payinfo['enginid']])->value('name');
            }
            $accInfo['jiaonum'] = (new IncomeService())->create_account_list_jiaonum_shou();
            $accInfo['money'] = $param['fumoney'];
            $accInfo['budid'] = $param['budid'];
            $accInfo['zhai'] = $payinfo['zhai'];
            $accInfo['faer'] = $payinfo['gonger'];
            $accInfo['daotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $param['incdaotype']);
            $accInfo['dt'] = $param['dt'];
            $accInfo['accid'] = $param['accid'];
            $accInfo['baner'] = $param['baner'];
            $accInfo['explain'] = $param['explain'];
            $accInfo['created_user'] = $this->userinfo['id'];
            $accInfo['updated_user'] = $this->userinfo['id'];
            $accInfo['created_at'] = date("Y-m-d H:i:s");
            $accInfo['updated_at'] = date("Y-m-d H:i:s");



            Db::startTrans();
            try {
                EnginBondShouModel::where(['id'=>$param['id']])->update(['budid'=>$param['budid']]);

                $id = AccountListModel::insertGetId($accInfo);

                (new OptService())->relation_file($files,'account_list',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'account_list', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'account_list', $id, '交易记录');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = EnginBondShouModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            if($data['data']['enginid'] > 0){
                $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            }else{
                $engininfo = BidModel::get_info_by_pk($data['data']['bidid']);
            }
            $data['engininfo'] = $engininfo;

            $data['data']['bondtype'] = (new PaymentService())->get_engin_bondtype_name($data['data']['bondtype']);

            $budshou = EnginBudshouModel::get_info_by_pk($data['data']['budid']);
            $data['budshou'] = $budshou;

            $data['dt'] = date("Y-m-d");
            $data['baner'] = $this->userinfo['realname'];
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
            $data['jiaonum'] = (new IncomeService())->create_account_list_jiaonum_shou();
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function edit_engin_bond_shou_kuan(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = AccountListModel::where(['id'=>$param['id']])->find();
            $payinfo = EnginBondShouModel::where(['id'=>$old['mid']])->find();


            if(empty($param['budid'])){
                return outputJson(4000, '请选择款项类型');
            }
            if(empty($param['fumoney'])){
                return outputJson(4000, '请输入本次到账金额');
            }
            if(empty($param['incdaotype'])){
                return outputJson(4000, '请选择到账方式');
            }
            if(empty($param['accid'])){
                return outputJson(4000, '请选择到账账户');
            }
            if(empty($param['dt'])){
                return outputJson(4000, '请选择到账日期');
            }
            if(empty($param['baner'])){
                return outputJson(4000, '请输入经手人');
            }


            $files = array_filter($param['files']);
            unset($param['files']);


            $accInfo = [];
            $accInfo['id'] = $param['id'];
            $accInfo['bidid'] = $payinfo['bidid'];
            $accInfo['enginid'] = $payinfo['enginid'];
            if($payinfo['bidid'] > 0){
                $accInfo['enginname'] = BidModel::where(['id'=>$payinfo['bidid']])->value('name');
            }else{
                $accInfo['enginname'] = EnginModel::where(['id'=>$payinfo['enginid']])->value('name');
            }
            $accInfo['money'] = $param['fumoney'];
            $accInfo['dt'] = $param['dt'];
            $accInfo['budid'] = $param['budid'];
            $accInfo['incdaotype'] = $param['incdaotype'];
            $accInfo['accid'] = $param['accid'];
            $accInfo['baner'] = $param['baner'];
            $accInfo['explain'] = $param['explain'];
            $accInfo['updated_user'] = $this->userinfo['id'];
            $accInfo['updated_at'] = date("Y-m-d H:i:s");


            Db::startTrans();
            try {
                AccountListModel::update($accInfo);

                (new OptService())->relation_file($files,'account_list',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'account_list', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'account_list', $param['id'], '交易记录');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = AccountListModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $payment = EnginBondShouModel::where(['id'=>$data['data']['mid']])->find();
            $payment['bondtype'] = (new PaymentService())->get_engin_bondtype_name($payment['bondtype']);
            $data['payment'] = $payment;

            if($payment['enginid'] > 0){
                $engininfo = EnginModel::get_info_by_pk($payment['enginid']);
            }else{
                $engininfo = BidModel::get_info_by_pk($payment['bidid']);
            }
            $data['engininfo'] = $engininfo;

            $account = AccountModel::get_info_by_pk($data['data']['accid']);
            $data['account'] = $account;


            $budshou = EnginBudshouModel::get_info_by_pk($payment['budid']);
            $data['budshou'] = $budshou;

            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
            $data['files'] = (new OptService())->get_file('account_list', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_engin_bond_shou_kuan(Request $request){
        $param = $request->post();
        $data = AccountListModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            AccountListModel::where(['id'=>$data['id']])->delete();
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'account_list', $data['id'], '交易记录');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }



    public function list_income_huan(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['huan.deleted_user','=', 0];
        $where[] = ['huan.comid','=',$this->userinfo['comid']];
        $where[] = ['huan.isturn','=',1];
        $where[] = ['huan.status','in',[0,1]];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['huan.number|jie.number|jie.gonger|huan.money|huan.zhai','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['huan.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['jienumber']) && !empty($param['jienumber'])){
            $where[] = ['jie.number','like', '%'.$param['jienumber'].'%'];
        }
        if(isset($param['jiegonger']) && !empty($param['jiegonger'])){
            $where[] = ['jie.gonger','like', '%'.$param['jiegonger'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['huan.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['huan.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['huan.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['huan.status','=', $param['status']];
        }


        $data['data'] = IncomeHuanModel::alias('huan')
            ->field('huan.*,jie.number as jienumber,jie.gonger as jiegonger')
            ->leftJoin('payment_jie jie','jie.id=huan.jieid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('income_huan', 'huan'))
            ->order('huan.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['accmoney'] = AccountListModel::where(['type'=>14,'mid'=>$item['id']])->sum('money');
                $item['daimoney'] = $item['money'] - $item['accmoney'];
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('income_huan');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_income_huan(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['jieid'])){
                return outputJson(4000, '请选择关联备用金');
            }
            if(empty($param['money'])){
                return outputJson(4000, '请输入还款金额');
            }
            if(empty($param['zhai'])){
                return outputJson(4000, '请输入还款说明');
            }
            if(empty($param['accid'])){
                return outputJson(4000, '请选择到账账户');
            }
            if(empty($param['dt'])){
                return outputJson(4000, '请选择还款日期');
            }


            $param['number'] = (new IncomeService())->create_income_huan_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['status'] = 1;
            $param['isturn'] = 1;
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            Db::startTrans();
            try {
                $id = IncomeHuanModel::insertGetId($param);
                (new OptService())->relation_file($files,'income_huan',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'income_huan', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'income_huan', $id, '还款');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['number'] = (new IncomeService())->create_income_huan_number();
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_income_huan(Request $request){
        if($request->isPost()){
            $param = $request->post();


            if(empty($param['money'])){
                return outputJson(4000, '请输入还款金额');
            }
            if(empty($param['zhai'])){
                return outputJson(4000, '请输入还款说明');
            }
            if(empty($param['accid'])){
                return outputJson(4000, '请选择到账账户');
            }
            if(empty($param['dt'])){
                return outputJson(4000, '请选择还款日期');
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            Db::startTrans();
            try {
                IncomeHuanModel::update($param);
                (new OptService())->relation_file($files,'income_huan',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'income_huan', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'income_huan', $param['id'], '还款');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = IncomeHuanModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $jieinfo = PaymentJieModel::get_info_by_pk($data['data']['jieid']);
            $jieinfo['incdaotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $jieinfo['incdaotype']);
            $data['jieinfo'] = $jieinfo;


            $account = AccountModel::get_info_by_pk($data['data']['accid']);
            $data['account'] = $account;

            $data['files'] = (new OptService())->get_file('income_huan', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_income_huan(Request $request){
        $param = $request->post();
        $data = IncomeHuanModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            IncomeHuanModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'income_huan', $data['id'], '还款');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_income_huan(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = IncomeHuanModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $jieinfo = PaymentJieModel::get_info_by_pk($data['data']['jieid']);
        $jieinfo['incdaotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $jieinfo['incdaotype']);
        $data['jieinfo'] = $jieinfo;



        $data['data']['payfee'] = AccountListModel::where(['type'=>14,'mid'=>$data['data']['id']])->sum('money');
        $data['data']['payfo_status'] = (new PaymentService())->get_paydata_huanstatus_name($data['data']['money'], $data['data']['payfee']);
        $data['data']['payfo_payfee'] = $data['data']['payfee'];
        $data['data']['payfo_dt'] = AccountListModel::where(['type'=>14,'mid'=>$data['data']['id']])->order('dt DESC')->value('dt');
        $data['data']['payfo_dai'] = $data['data']['money'] - $data['data']['payfee'];



        $data['files'] = (new OptService())->get_file('income_huan', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }
    public function list_income_huan_kuan_detail(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['list.comid','=',$this->userinfo['comid']];
        $where[] = ['list.type','=',14];
        $where[] = ['list.mid','=',$param['mid']];


        if(isset($param['jiaonum']) && !empty($param['jiaonum'])){
            $where[] = ['list.jiaonum','like', '%'.$param['jiaonum'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['list.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['list.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['faer']) && !empty($param['faer'])){
            $where[] = ['list.faer','like', '%'.$param['faer'].'%'];
        }
        if(isset($param['daotype']) && !empty($param['daotype'])){
            $where[] = ['list.daotype','like', '%'.$param['daotype'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['list.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['accname']) && !empty($param['accname'])){
            $where[] = ['acc.accname','like', '%'.$param['accname'].'%'];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['list.baner','like', '%'.$param['baner'].'%'];
        }


        $data['data'] = AccountListModel::alias('list')
            ->field('list.*,acc.name as accname,acc.accnum')
            ->leftJoin('account acc','acc.id=list.accid')
            ->where($where)
            ->order('list.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['budid_name'] = (new BudgetService())->get_budid_quanname($item['budid']);
                $item['accname'] = $item['accname'].'('.$item['accnum'].')';
                return $item;
            })->toArray();

        $data['midinfo'] = IncomeHuanModel::where(['id'=>$param['mid']])->find();
        if($data['midinfo']){
            $data['midinfo']['accmoney'] = AccountListModel::where(['type'=>14,'mid'=>$data['midinfo']['id']])->sum('money');
            $data['midinfo']['daimoney'] = $data['midinfo']['money'] - $data['midinfo']['accmoney'];
        }
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_income_huan_kuan(Request $request){
        if($request->isPost()){
            $param = $request->post();

            $payinfo = IncomeHuanModel::where(['id'=>$param['id']])->find();
            $jie = PaymentJieModel::where(['id'=>$payinfo['jieid']])->find();

            if(empty($param['fumoney'])){
                return outputJson(4000, '请输入本次到账金额');
            }
            if(empty($param['budid'])){
                return outputJson(4000, '请选择款项类型');
            }
            if(empty($param['incdaotype'])){
                return outputJson(4000, '请选择到账方式');
            }
            if(empty($param['accid'])){
                return outputJson(4000, '请选择到账账户');
            }
            if(empty($param['dt'])){
                return outputJson(4000, '请选择到账日期');
            }
            if(empty($param['baner'])){
                return outputJson(4000, '请输入经手人');
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);

            $accInfo = [];
            $accInfo['comid'] = $this->userinfo['comid'];
            $accInfo['type'] = 14;
            $accInfo['mid'] = $param['id'];
            $accInfo['jiaonum'] = (new IncomeService())->create_account_list_jiaonum_shou();
            $accInfo['money'] = $param['fumoney'];
            $accInfo['budid'] = $param['budid'];
            $accInfo['zhai'] = $payinfo['zhai'];
            $accInfo['faer'] = $jie['gonger'];
            $accInfo['daotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $param['incdaotype']);
            $accInfo['dt'] = $param['dt'];
            $accInfo['accid'] = $param['accid'];
            $accInfo['baner'] = $param['baner'];
            $accInfo['explain'] = $param['explain'];
            $accInfo['created_user'] = $this->userinfo['id'];
            $accInfo['updated_user'] = $this->userinfo['id'];
            $accInfo['created_at'] = date("Y-m-d H:i:s");
            $accInfo['updated_at'] = date("Y-m-d H:i:s");



            Db::startTrans();
            try {

                $id = AccountListModel::insertGetId($accInfo);

                (new OptService())->relation_file($files,'account_list',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'account_list', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'account_list', $id, '交易记录');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = IncomeHuanModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }


            $jieinfo = PaymentJieModel::get_info_by_pk($data['data']['jieid']);
            $jieinfo['incdaotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $jieinfo['incdaotype']);
            $data['jieinfo'] = $jieinfo;

            $data['subarr'] = (new BudgetService())->get_engin_budshou_qi_all_xiala();
            $data['dt'] = date("Y-m-d");
            $data['baner'] = $this->userinfo['realname'];
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
            $data['jiaonum'] = (new IncomeService())->create_account_list_jiaonum_shou();
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function edit_income_huan_kuan(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = AccountListModel::where(['id'=>$param['id']])->find();
            $payinfo = IncomeHuanModel::where(['id'=>$old['mid']])->find();


            if(empty($param['budid'])){
                return outputJson(4000, '请选择款项类型');
            }
            if(empty($param['fumoney'])){
                return outputJson(4000, '请输入本次到账金额');
            }
            if(empty($param['incdaotype'])){
                return outputJson(4000, '请选择到账方式');
            }
            if(empty($param['accid'])){
                return outputJson(4000, '请选择到账账户');
            }
            if(empty($param['dt'])){
                return outputJson(4000, '请选择到账日期');
            }
            if(empty($param['baner'])){
                return outputJson(4000, '请输入经手人');
            }


            $files = array_filter($param['files']);
            unset($param['files']);


            $accInfo = [];
            $accInfo['id'] = $param['id'];
            $accInfo['money'] = $param['fumoney'];
            $accInfo['dt'] = $param['dt'];
            $accInfo['budid'] = $param['budid'];
            $accInfo['incdaotype'] = $param['incdaotype'];
            $accInfo['accid'] = $param['accid'];
            $accInfo['baner'] = $param['baner'];
            $accInfo['explain'] = $param['explain'];
            $accInfo['updated_user'] = $this->userinfo['id'];
            $accInfo['updated_at'] = date("Y-m-d H:i:s");


            Db::startTrans();
            try {
                AccountListModel::update($accInfo);

                (new OptService())->relation_file($files,'account_list',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'account_list', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'account_list', $param['id'], '交易记录');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = AccountListModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $payment = IncomeHuanModel::where(['id'=>$data['data']['mid']])->find();
            $data['payment'] = $payment;

            $jieinfo = PaymentJieModel::get_info_by_pk($payment['jieid']);
            $jieinfo['incdaotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $jieinfo['incdaotype']);
            $data['jieinfo'] = $jieinfo;

            $account = AccountModel::get_info_by_pk($data['data']['accid']);
            $data['account'] = $account;


            $budshou = EnginBudshouModel::get_info_by_pk($data['data']['budid']);
            $data['budshou'] = $budshou;
            $data['subarr'] = (new BudgetService())->get_engin_budshou_qi_all_xiala();
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
            $data['files'] = (new OptService())->get_file('account_list', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_income_huan_kuan(Request $request){
        $param = $request->post();
        $data = AccountListModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            AccountListModel::where(['id'=>$data['id']])->delete();
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'account_list', $data['id'], '交易记录');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }



    public function list_income_incshou(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['inc.comid','=',$this->userinfo['comid']];


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['inc.number|e.name|inc.faer|inc.zhai|user.realname|acc.accname','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['inc.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['paytype']) && !empty($param['paytype'])){
            $where[] = ['inc.paytype','=', $param['paytype']];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['accname']) && !empty($param['accname'])){
            $where[] = ['acc.accname','like', '%'.$param['accname'].'%'];
        }
        if(isset($param['faer']) && !empty($param['faer'])){
            $where[] = ['inc.faer','like', '%'.$param['faer'].'%'];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['inc.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['incqing']) && !empty($param['incqing'])){
            $where[] = ['inc.incqing','=', $param['incqing']];
        }
        if(isset($param['incdaotype']) && !empty($param['incdaotype'])){
            $where[] = ['inc.incdaotype','=', $param['incdaotype']];
        }
        if(isset($param['created_user_name']) && !empty($param['created_user_name'])){
            $where[] = ['user.realname','like', '%'.$param['created_user_name'].'%'];
        }


        $data['paytype'] = (new IncomeService())->get_incshou_paytype();
        $data['is_paytype'] = isset($param['paytype']) ? $param['paytype'] : '';

        $data['incqing'] = (new OptService())->get_base_info('cai_income_incqing');
        $data['is_incqing'] = isset($param['incqing']) ? $param['incqing'] : '';

        $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
        $data['is_incdaotype'] = isset($param['incdaotype']) ? $param['incdaotype'] : '';



        $data['data'] = IncshouModel::alias('inc')
            ->field('inc.*,e.name as enginname,user.realname as created_user_name,bud.name as budid_name,acc.name as accname,acc.accnum')
            ->leftJoin('engin e','e.id=inc.enginid')
            ->leftjoin('user user','user.id=inc.created_user')
            ->leftJoin('engin_budshou bud','bud.id=inc.budid')
            ->leftJoin('account acc','acc.id=inc.accid')
            ->where($where)
            ->order('inc.created_at DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['paystatus'] = '已入账';
                $item['accname'] = $item['accname'].'('.$item['accnum'].')';
                $item['paytype_name'] = (new IncomeService())->get_incshou_paytype_name($item['paytype']);
                $item['incqing'] = (new OptService())->get_base_showname('cai_income_incqing', $item['incqing']);
                $item['incdaotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $item['incdaotype']);
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }


}
